<template>
    <div class="photo-con">
        <!-- 顶部的横向滑动区域 -->
        <div id="slider" class="mui-slider">
            <div id="sliderSegmentControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <div class="mui-scroll">
                    <a :class="['mui-control-item',cateId===item.id?'mui-active':'']"  v-for="item in cates" :key="item.id"
                    @tap="getPhotoListByCateId(item.id)">{{item.title}}</a>
                </div>
            </div>
        </div>
        <!-- 图片列表 -->
        <ul class="photo-list">
            <li v-for="item in list" :key="item.id">
                <router-link :to="{name:'photo_info', params: {id:item.id}}">
                    <img v-lazy="item.img_url">
                    <div class="info">
                        <h1 class="info-title">{{item.title}}</h1>
                        <div class="info-body">{{item.zhaiyao}}</div>
                    </div>
                </router-link>
            </li>
        </ul>
    </div>
</template>

<script>
export default {
    data() {
        return {
            cates: [],  // 保存所有的分类
            list: [],   // 保存图片的列表
            cateId: 0   // 保存需要查询的分类id
        }
    },
    created() {
        this.getAllCategory();
        this.getPhotoListByCateId(0); // 0 为查询全部图片
    },
    methods: {
        getAllCategory() {
            // 获取所有图片分享分类
            this.$http.get('photo/getimgcategory').then(res => {
                if (res.data.status === 0) {
                    this.cates = res.data.message;
                    this.cates.unshift({title: "全部", id: 0});
                    console.log(this.cates);
                } else {
                    this.$toast('加载失败');
                }
            })
        },
        getPhotoListByCateId(cateId) {
            this.cateId = cateId;
            // 根据分类id 请求图片数据
            this.$http.get('photo/getimages', {params: {id: this.cateId}}).then(res => {
                if (res.data.status === 0) {
                    this.list = res.data.message;
                    console.log(this.list);
                } else {
                    this.$toast('查询失败');
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.photo-con {
    .photo-list {
        margin: 0;
        padding: 10px;
        padding-bottom: 0;
        list-style: none;
        li {
            position: relative;
            text-align: left;
            width: 100%;
            font-size: 14px;
            box-shadow: 0 0 9px #999;
            img {
                width: 100%;
            }
            .info {
                position: absolute;
                bottom: 4px;
                left: 0;
                color: #fff;
                background-color: rgba(0,0,0,.3);
                .info-title {
                    font-size: 14px;
                }
            }
        }
    }
}
</style>